import os
import os.path


def list_dir(dir_path, recursive=False, filter_func=None):
    if filter_func is None:
        filter_func = lambda x: True
    if os.path.isfile(dir_path):
        if filter_func(dir_path):
            yield dir_path
    elif not os.path.isdir(dir_path):
        return
    try:
        for child in os.listdir(dir_path):
            child_path = os.path.join(dir_path, child)
            if os.path.isdir(child_path) and recursive:
                yield from list_dir(child_path, True, filter_func)
            elif os.path.isfile(child_path) and filter_func(child_path):
                yield child_path
    except:
        pass